package hlhj.fhp.supreme.customview

import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.widget.LinearLayout
import android.widget.PopupWindow
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.JiePanCatBean
import kotlinx.android.synthetic.main.class_type_pop.view.*

/**
 * Created by Never Fear   on 2018\7\19 0019.
Never More....
 */
class ChooseJiePanPop(private val context: Context,private val datas:ArrayList<JiePanCatBean.DataBean>,private val listener:OnChooseListener) :PopupWindow() {
    private lateinit var adp:BaseQuickAdapter<JiePanCatBean.DataBean,BaseViewHolder>
    private val selectHash=HashMap<Int,Boolean>()
    init {
        initPop()
        this.width= LinearLayout.LayoutParams.WRAP_CONTENT
        this.height= LinearLayout.LayoutParams.WRAP_CONTENT
    }

    private fun initPop() {
        animationStyle= R.style.class_pop_style
        contentView= LayoutInflater.from(context).inflate(R.layout.class_type_pop,null)
        for (i in 0..datas.size-1){
            selectHash.put(i,false)
        }
        adp=object :BaseQuickAdapter<JiePanCatBean.DataBean,BaseViewHolder>(R.layout.appointment_item,datas){
            override fun convert(helper: BaseViewHolder?, item: JiePanCatBean.DataBean?) {
                helper?.setText(R.id.itemTittle,item?.name)
                val view = helper?.getView<TextView>(R.id.itemTittle)
                if (selectHash[helper?.adapterPosition]!!){
                    view?.setBackgroundColor(context.resources.getColor(R.color.them_yellow))
                }else{
                    view?.setBackgroundColor(context.resources.getColor(R.color.them_unsele))
                }
            }
        }
        contentView.listView.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)
        contentView.listView.adapter=adp
        adp.setOnItemClickListener { adapter, view, position ->
            listener.choose(datas[position].name,datas[position].id.toString())
            setSele(position)
            dismiss()
        }
    }
    public fun setSele(num:Int){
        for (i in 0..datas.size-1){
            if (i==num){
                selectHash.put(i,true)
            }else{
                selectHash.put(i,false)
            }

        }
        adp.notifyDataSetChanged()
    }
    interface OnChooseListener{
        fun choose(name:String,id:String)
    }
}